package hn.sigit.logic.security;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.jboss.seam.annotations.intercept.Interceptor;
import org.jboss.seam.contexts.Contexts;

@Interceptor(stateless=true)
public class LoggedInInterceptor {
	@AroundInvoke
	public Object checkLoggedIn(InvocationContext invocation) throws Exception {
		boolean isLoggedIn = Contexts.getSessionContext().get("loggedIn") != null;
		
		System.out.println("Se intercepto la llamada a: " + invocation.getMethod().getName());
		System.out.println("Loginead (antes de llamada): " + isLoggedIn);
		
		Boolean result = (Boolean)invocation.proceed();
		
		isLoggedIn = Contexts.getSessionContext().get("loggedIn") != null;
		System.out.println("Loginead (antes de llamada): " + isLoggedIn);

		System.out.println("Resultado: " + result);
		System.out.println("Tipo de retorno: " + result.getClass().getName());
		
		if (result)
			return result;
		
		return "login";
		/*
		boolean isLoggedIn = Contexts.getSessionContext().get("loggedIn") != null;
		if (isLoggedIn) {
			return invocation.proceed();
		} else {
			// the user is not logged in, fwd to login page
			return "login";
		}
		*/
	}

}
